package com.test.sdk;

import java.util.concurrent.locks.ReentrantLock;

public class OrderUtil {
    private static final ReentrantLock orderIdLock = new ReentrantLock();

    private static int serialNum = 0;

    private static final char[] CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
            'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
            'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    public static String getOrderId() {
        StringBuilder sb = new StringBuilder();
        sb.append(System.currentTimeMillis());
        try {
            orderIdLock.lock();
            int m = serialNum / CHARS.length;
            int n = serialNum % CHARS.length;
            sb.append(CHARS[m]);
            sb.append(CHARS[n]);
            if (serialNum >= (62 * 62 - 1)) {
                serialNum = 0;
            } else {
                serialNum++;
            }
        } finally {
            orderIdLock.unlock();
        }
        return sb.toString();
    }

//    public static void main(String[] args) {
//        for (int i = 0; i < 100; i++) {
//            Runnable runnable = new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println(OrderUtil.getOrderId());
//                }
//            };
//            new Thread(runnable).start();
//        }
//    }
}
